package com.redlight.base;

import com.alibaba.fastjson.JSONObject;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chao on 2019/1/9.
 */
public class RestResult extends HashMap {
    public RestResult(boolean _success) {
        this(_success, (Object)null, new Object[0]);
    }

    public RestResult(ResultStatus status) {
        this(status.isSuccess(), status.getMessage(), new Object[0]);
        this.put("retCode", status.getCode());
    }

    public RestResult(boolean _success, Object data, Object... args) {
        this(_success, _success?ResultStatus.OK.getCode():ResultStatus.FAIL.getCode(), data, args);
    }

    public RestResult(boolean _success, String _retCode, Object data, Object... args) {
        this.put("success", Boolean.valueOf(_success));
        this.put("retCode", _retCode);
        if(null != data) {
            if(data instanceof Map) {
                this.putAll((Map)data);
            } else if(data instanceof Collection) {
                this.put("rows", data);
            } else if(data instanceof Page) {
                this.put("page", data);
            } else if(data instanceof BaseBean) {
                this.put(data.getClass().getSimpleName(), data);
            } else if(data instanceof JSONObject) {
                this.putAll((Map)JSONObject.toJavaObject((JSONObject)data, Map.class));
            } else {
                this.put("message", data);
            }
        }

        if(args != null && 0 < args.length) {
            for(int i = 0; i < args.length; ++i) {
                this.put("result" + (i + 1), args[i]);
            }
        }

        if(this.get("message") == null) {
            this.put("message", _success?ResultStatus.OK.getMessage():ResultStatus.FAIL.getMessage());
        }

    }

    public static RestResult ok() {
        return new RestResult(ResultStatus.OK);
    }

    public static RestResult fail() {
        return new RestResult(ResultStatus.FAIL);
    }

    public boolean getSuccess() {
        return this.get("success") != null?((Boolean)this.get("retCode")).booleanValue():false;
    }

    public String getRetCode() {
        return this.get("retCode") != null?this.get("retCode").toString():"";
    }

    public void setRetCode(String _retCode) {
        this.put("retCode", _retCode);
    }

    public String getMessage() {
        return this.get("message") != null?this.get("message").toString():"";
    }

    public void setMessage(String _message) {
        this.put("message", _message);
    }

    public Collection getRows() {
        return (Collection)this.get("rows");
    }

    public void setRows(Collection _rows) {
        this.put("rows", _rows);
    }

    public Page getPage() {
        return (Page)this.get("page");
    }

    public void setPage(Page _page) {
        this.put("page", _page);
    }

    public RestResult add(String key, Object val) {
        this.put(key, val);
        return this;
    }
}
